package com.gouaixin.bee.beeframework2.model;

import android.content.Context;

import com.ab.util.AbToastUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gouaixin.bee.beeframework2.MyAPI;
import com.gouaixin.bee.beeframework2.bean.FANGKE;
import com.gouaixin.bee.beeframework2.bean.GUANZHU;
import com.gouaixin.bee.beeframework2.dao.FangkeDao;
import com.gouaixin.bee.beeframework2.bean.BaseModel;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;
import com.lidroid.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/4/8.
 */
//关注功能控制器
public class AttentionModel {

//添加关注
    public static void  addGuanzhu(Context context,String jid,String jicon,String cid){
        //添加关注对象到网络关注表
        BaseModel.setCallBackLister(new BaseModel.CallBackLister() {
            @Override
            public String onCallBack(String url, String data) {
                return url;
            }
        });
        RequestParams params = new RequestParams();
        params.addBodyParameter("id", jid);
        params.addBodyParameter("cid", cid);
        BaseModel.post(MyAPI.addguanzhu, params);
        //添加关注对象到本地访客表
        FANGKE bean = new FANGKE();
        bean.setJid(jid);
        bean.setJicon(jicon);
        FangkeDao.add(context, bean);



        //配置缓存信息
        PreferencesUtils.putBoolean(context, jid + "isGuanzhu", true);
        mLog.e(jid + "isGuanzhu=" + PreferencesUtils.getBoolean(context, jid + "isGuanzhu"));

    }
//移除关注
    public static void  removeGuanzhu(Context context,String jid,String cid){
        //移除关注对象到网络关注表
        BaseModel.setCallBackLister(new BaseModel.CallBackLister() {
            @Override
            public String onCallBack(String url, String data) {

                return url;
            }
        });
        RequestParams params = new RequestParams();
        params.addBodyParameter("id", jid);
        params.addBodyParameter("cid", cid);
        BaseModel.post(MyAPI.removeguanzhu, params);

        //配置缓存信息
        AbToastUtil.showToast(context, "取消关注成功");
        PreferencesUtils.putBoolean(context, jid + "isGuanzhu", false);

    }
//查看关注列表
    public static List<GUANZHU> getGuanzhuList(Context context,String cid,int p){
        List<GUANZHU> list=new ArrayList<GUANZHU>();
        //获取关注对象列表从网络关注表
        BaseModel.setCallBackLister(new BaseModel.CallBackLister() {
            @Override
            public String onCallBack(String url, String data) {
                data=new Gson().fromJson(data, new TypeToken<ArrayList<GUANZHU>>(){}.getType());
                return url;
            }
        });
        RequestParams params = new RequestParams();
        params.addBodyParameter("cid", cid);
        params.addBodyParameter("p", p+"");
        BaseModel.post(MyAPI.removeguanzhu, params);

        return list;
    }


}
